import axios from 'axios'
import config from '../config'

export function ps (query, size = 999) {
  let url = config.SERVER_URL + window.apiPath + `/ships-ps?size=${size}&query=${query}`
  return axios.get(url).then(resp => resp.data)
}

export function getDetial (mmsi, shipNameEn = '') {
 // mmsi = '413452980'
  let url = config.SERVER_URL + window.apiPath + '/ships-s-mmsi_enname?mmsi=' + mmsi + '&shipNameEn=' + shipNameEn
  return axios.get(url).then(resp => resp.data)
}

export function search (query) {
  let url = config.SERVER_URL + window.apiPath + '/ships-s?query=' + query
  return axios.get(url).then(resp => resp.data)
}

export function searchShips (query, page = 0, size = config.PAGE_SIZE) {
  let url = config.SERVER_URL + window.apiPath + '/ships-ps?query=' + query + `&page=${page}&size=${size}`
  return axios.get(url).then(resp => resp)
}

export function searchPS (query, page = 0, size = config.PAGE_SIZE) {
  let url = config.SERVER_URL + '/api/ships-search?' + query + `&page=${page}&size=${size}`
  return axios.get(url).then(resp => resp.data)
}

export function findLocationHis (terminalCode, searchStartTime, searchEndTime, gpsCount = 30) {
  // let url = config.SERVER_URL + window.apiPath + '/ships-findlocationhis?terminalCode=' + terminalCode + '&searchStartTime=' + searchStartTime + '&searchEndTime=' + searchEndTime
  let url = config.SERVER_URL + window.apiPath + `/ships-history?gpsCount=${gpsCount}&terminalCode=${terminalCode}&searchStartTime=${searchStartTime}&searchEndTime=${searchEndTime}`
  // let url = config.SERVER_URL + '/ais/location-his/findlocationhis?terminalCode=' + terminalCode + '&searchStartTime=' + searchStartTime + '&searchEndTime=' + searchEndTime
  return axios.get(url).then(resp => resp.data)
}

export function getShipAis (mmsi) {
  let url = config.SERVER_URL + '/ais/ship-locations/' + mmsi
  return axios.get(url).then(resp => {
    if (resp.data && resp.data.length) {
      return resp.data[0]
    }
    return {}
  })
}

export function findLocationAll (query) {
  let url = config.SERVER_URL + '/ais/ship-locations?'+query;
  return axios.get(url).then(resp => resp.data)
}
